package com.health.db.service.user.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.health.commons.PropertiesService;
import com.health.db.dao.user.ICourseShareJournalDao;
import com.health.db.dao.user.ICourseShareLedgerDao;
import com.health.db.dao.user.ICourseShareRemarkDao;
import com.health.db.dao.user.ICourseShareVoucherBillDao;
import com.health.db.model.CourseShareJournal;
import com.health.db.model.CourseShareLedger;
import com.health.db.model.CourseShareLedgerExample;
import com.health.db.model.CourseShareRemark;
import com.health.db.model.CourseShareRemarkExample;
import com.health.db.model.CourseShareVoucherBill;
import com.health.db.model.GoodsOrder;
import com.health.db.service.user.ICourseShareLedgerService;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.DateUtils;

@Component("courseShareLedgerService")
public class CourseShareLedgerServiceImpl implements ICourseShareLedgerService {
	
	@Autowired
	ICourseShareLedgerDao courseShareLedgerDao ;
	
	@Autowired
	ICourseShareJournalDao courseShareJournalDao ;
	
	@Autowired
	ICourseShareRemarkDao courseShareRemarkDao;
	
	@Autowired
	ICourseShareVoucherBillDao courseShareVoucherBillDao ;

	@Override
	public CourseShareLedger selectByUserId(Long recid) {
		// TODO Auto-generated method stub
		return courseShareLedgerDao.selectByUserId(recid);
	}

	@Override
	public int insert(CourseShareLedger record) {
		// TODO Auto-generated method stub
		return courseShareLedgerDao.insert(record);
	}

	@Override
	public int updateByPrimaryKeySelective(CourseShareLedger record) {
		// TODO Auto-generated method stub
		return courseShareLedgerDao.updateByPrimaryKeySelective(record);
	}

	public ICourseShareLedgerDao getCourseShareLedgerDao() {
		return courseShareLedgerDao;
	}

	public void setCourseShareLedgerDao(ICourseShareLedgerDao courseShareLedgerDao) {
		this.courseShareLedgerDao = courseShareLedgerDao;
	}

	public ICourseShareJournalDao getCourseShareJournalDao() {
		return courseShareJournalDao;
	}

	public void setCourseShareJournalDao(
			ICourseShareJournalDao courseShareJournalDao) {
		this.courseShareJournalDao = courseShareJournalDao;
	}

	/**
	 * 提现日志
	 */
	@Override
	public CourseShareLedgerExample insertCourseShareCash(CourseShareLedger record,String paymentId,BigDecimal ledgeTotal) {
		// TODO Auto-generated method stub
		CourseShareLedgerExample returnExample = new CourseShareLedgerExample();
		
		Date now=DateUtils.now();
		record.setUpdateDate(now);
		
		 courseShareLedgerDao.updateByPrimaryKeySelective(record);
		
		CourseShareJournal journal = new CourseShareJournal();
		
		journal.setJournalDesc(Constant.CONSTANTS_VOUCHERNAME_62+"提现金额:"+ledgeTotal.toPlainString());
		journal.setVoucherName(Constant.CONSTANTS_VOUCHERNAME_62);
		journal.setAccountid(record.getAccountid());
		journal.setJournalTotal(ledgeTotal.multiply(new BigDecimal(-1)));
		journal.setCreateUserid(record.getUpdateUserid());
		journal.setUpdateUserid(record.getUpdateUserid());
		journal.setVoucherDate(now);
		journal.setJournalDate(now);
		journal.setVouchertypeid(Constant.CONSTANTS_PAY_ACTION_ID_61);
		journal.setSourceid(Constant.CONSTANTS_PAY_ACTION_ID_61);
		journal.setComment(paymentId);
		courseShareJournalDao.insert(journal);
		
		
		CourseShareVoucherBill	 voucherBill = new CourseShareVoucherBill();
		voucherBill.setRunStatus("已完成");
		voucherBill.setUserid(record.getUpdateUserid());
		voucherBill.setAccountid(record.getAccountid());
		voucherBill.setUpdateUserid(record.getUpdateUserid());
		voucherBill.setCreateDate(now);
		voucherBill.setUpdateDate(now);
		voucherBill.setRunFlag((short) 9);
		voucherBill.setBillTotal(ledgeTotal);
		courseShareVoucherBillDao.insert(voucherBill);
		
		returnExample.setCourseShareJournalId(journal.getRecid());
		returnExample.setCourseShareVoucherBillId(voucherBill.getRecid());
		
		return returnExample;
	}

	@Override
	public int reset(CourseShareLedger record, CourseShareLedgerExample courseShareLedgerExample) {
		
		Date now=DateUtils.now();
		record.setUpdateDate(now);
		
		int result=courseShareLedgerDao.updateByPrimaryKeySelective(record);
		
		CourseShareJournal journal = new CourseShareJournal();
		journal.setRecid(courseShareLedgerExample.getCourseShareJournalId());
		journal.setAccountid(record.getAccountid());
		courseShareJournalDao.delete(journal);
		
		CourseShareVoucherBill	 voucherBill = new CourseShareVoucherBill();
		voucherBill.setRecid(courseShareLedgerExample.getCourseShareVoucherBillId());
		voucherBill.setAccountid(record.getAccountid());
		
		courseShareVoucherBillDao.delete(voucherBill);
		
		
		return result;
	}

	/**
	 * 只做一级代理
	 */
	@SuppressWarnings("unused")
	@Override
	public void updateCourseShareLedgePayment(GoodsOrder record, String out_trade_no) {
		// TODO Auto-generated method stub
				
			//用戶id 时间
			Long buyerUserId = record.getOwnerid();
			
			CourseShareRemarkExample example = new CourseShareRemarkExample();
			example.setUserId(buyerUserId);
			example.setGoodsId(record.getGoodsid());
			
			
			//查询邀请人
			List<CourseShareRemark> remarkList = courseShareRemarkDao.selectByExample(example);
			
			if (remarkList.size()>0) {
				
				Long userId =remarkList.get(0).getInviterId(); 
				
				BigDecimal  balanceTotal = new BigDecimal("5");
				
				BigDecimal  balanceTotalNormal= record.getTotal().multiply(new BigDecimal(PropertiesService.getString("miniprogram.dividePercent")));
				
				
				if (null==userId) {
					return;
				}
				Date now=DateUtils.now();
			
				CourseShareJournal journal = new CourseShareJournal();
				//支付成功
				if (record.getGoodsid().longValue()==62) {
					journal.setJournalDesc(Constant.CONSTANTS_VOUCHERNAME_61+"分享金额:"+balanceTotal.toPlainString());
				}else{
					journal.setJournalDesc(Constant.CONSTANTS_VOUCHERNAME_61+"分享金额:"+balanceTotalNormal.toPlainString());
				}
				
				journal.setVoucherName(Constant.CONSTANTS_VOUCHERNAME_61);
				
				journal.setAccountid(userId);
				journal.setJournalTotal(record.getTotal());
				journal.setCreateUserid(userId);
				journal.setUpdateUserid(userId);
				journal.setVoucherDate(now);
				journal.setJournalDate(now);
				journal.setVouchertypeid(Constant.CONSTANTS_PAY_ACTION_ID_61);
				journal.setSourceid(Constant.CONSTANTS_PAY_ACTION_ID_61);
				journal.setComment(out_trade_no);
				journal.setVoucherid(record.getRecid().toString());
				
				//商户订单号 作为备注
				journal.setComment(out_trade_no);
				
				CourseShareLedger courseShareLedger = courseShareLedgerDao.selectByUserId(userId);
				
				if (null!=courseShareLedger) {
					
					CourseShareLedger updateCourseShareLedger = new CourseShareLedger();
					updateCourseShareLedger.setUpdateDate(now);
					updateCourseShareLedger.setAccountid(userId);
					updateCourseShareLedger.setUpdateUserid(userId);
				//	updateCourseShareLedger.setBalanceTotal(courseShareLedger.getBalanceTotal().add(record.getTotal().multiply(new BigDecimal(PropertiesService.getString("miniprogram.dividePercent")))));
					
					if (record.getGoodsid().longValue()==62) {
						updateCourseShareLedger.setBalanceTotal(courseShareLedger.getBalanceTotal().add(balanceTotal));
					}else{
						updateCourseShareLedger.setBalanceTotal(courseShareLedger.getBalanceTotal().add(balanceTotalNormal));
					}
					
					courseShareLedgerDao.updateByPrimaryKeySelective(updateCourseShareLedger);
					
				}else{
					courseShareLedger = new CourseShareLedger();
					
					if (record.getGoodsid().longValue()==62) {
						courseShareLedger.setBalanceTotal(balanceTotal);
					}else{
						courseShareLedger.setBalanceTotal(balanceTotalNormal);
					}
					
					
					courseShareLedger.setAccountid(userId);
					courseShareLedger.setIsUsedFlag((short) 1);
					courseShareLedger.setCreateDate(now);
					courseShareLedger.setCreateUserid(userId);
					courseShareLedger.setUpdateDate(now);
					courseShareLedger.setUpdateUserid(userId);
					courseShareLedgerDao.insert(courseShareLedger);
				}
				
				courseShareJournalDao.insert(journal);
			}
			
	}

	public ICourseShareRemarkDao getCourseShareRemarkDao() {
		return courseShareRemarkDao;
	}

	public void setCourseShareRemarkDao(ICourseShareRemarkDao courseShareRemarkDao) {
		this.courseShareRemarkDao = courseShareRemarkDao;
	}

	public ICourseShareVoucherBillDao getCourseShareVoucherBillDao() {
		return courseShareVoucherBillDao;
	}

	public void setCourseShareVoucherBillDao(
			ICourseShareVoucherBillDao courseShareVoucherBillDao) {
		this.courseShareVoucherBillDao = courseShareVoucherBillDao;
	}
}
